package com.hundsun.fcp.webplugin.service.update;

import java.net.SocketTimeoutException;

import org.apache.http.Header;
import org.apache.http.client.HttpResponseException;
import org.apache.http.conn.ConnectTimeoutException;

import android.content.Context;

import com.hundsun.fcp.common.logger.AndroidLogger;
import com.hundsun.fcp.common.logger.Logger;
import com.hundsun.fcp.webplugin.service.ErrorCode;
import com.hundsun.fcp.webplugin.service.FcpDataServiceBase;

public class FcpDownloadBinaryDataService extends FcpDataServiceBase {
	private static final Logger LOGGER = AndroidLogger.getLogger() ;
	private String mDownloadingUrl ;
	

	protected FcpDownloadBinaryDataService(Context pContext) {
		super(pContext);
		getAsyncHttpClient().addHeader("Cache-control", "private");
	    getAsyncHttpClient().addHeader("Pragma", "private");
	}
	
	public static FcpDownloadBinaryDataService create(Context pContext)
	{
	    return new FcpDownloadBinaryDataService(pContext);
	}

	@Override
	public void get(Object pObject) {
	    getAsyncHttpClient().get(getContext(), getRequestURL(null), this);
	}

	@Override
	protected String getRequestURL(String url) {
		return mDownloadingUrl;
	}
	
	public void setDownloadUrl(String pDownloadingUrl)
	  {
	    this.mDownloadingUrl = pDownloadingUrl ;
	  }

	@Override
	public void post(Object pObject) {
		getAsyncHttpClient().post(getContext(), getRequestURL(null), null, this);
	}

	@Override
	public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
		try {
			if (getCallBack() != null) {
			    getCallBack().onGetData(responseBody);
			}
		} catch (Exception e) {
			LOGGER.debug("下载大数据文件处理异常", e) ;
			HandleError(ErrorCode.ON_SUCCESS_ERROR, e.getMessage(), e);
		}

	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			byte[] responseBody, Throwable error) {
		int _ErrorCode = ErrorCode.SOCKET_ERROR ;
		try{
			
		    if (error instanceof HttpResponseException) {
		    	_ErrorCode = ((HttpResponseException)error).getStatusCode();
		    }
		    
		    if(error instanceof SocketTimeoutException){
		    	_ErrorCode = ErrorCode.SOCKET_TIMEOUT ;
		    }
		    
		    if(error instanceof ConnectTimeoutException){
		    	_ErrorCode = ErrorCode.CONNECT_TIMEOUT ;
		    }

		    String errorMsg = new String(responseBody);
			
		    HandleError(_ErrorCode, errorMsg==null?"":errorMsg, error);
		    
		}catch(Exception e){
			 HandleError(_ErrorCode, "网络连接连接错误", error);
		}
		 
	}
	
	 public void onFinish()
	 {
	    super.onFinish();
	    LOGGER.debug("Downloading Finished..") ;
	    if ((getCallBack() != null) && IFcpDownloadBinaryDataServiceHandler.class.isAssignableFrom(getCallBack().getClass())) {
	      ((IFcpDownloadBinaryDataServiceHandler)getCallBack()).onFinish();
	    }
	 }
	 
	 @Override
	public void onStart() {
		super.onStart();
		if ((getCallBack() != null) && IFcpDownloadBinaryDataServiceHandler.class.isAssignableFrom(getCallBack().getClass())) {
			((IFcpDownloadBinaryDataServiceHandler)getCallBack()).onStart();
		}
	}
	 
	 @Override
	public void onProgress(int bytesWritten, int totalSize) {
		super.onProgress(bytesWritten, totalSize);
		
		 LOGGER.debug(String.format("bytesWritten:%d, totalSize:%d", bytesWritten,totalSize));
		    if ((getCallBack() != null) && 
		    		IFcpDownloadBinaryDataServiceHandler.class.isAssignableFrom(getCallBack().getClass())) {
		      ((IFcpDownloadBinaryDataServiceHandler)getCallBack()).onProgress(bytesWritten, totalSize);
		    }
	}

	

}
